<?php
/**
 * 离职图表
 */
apf_require_controller("Abstract");
apf_require_class("Bll_Report_Report");
apf_require_class("Report_Urls");
class Report_DimissionController extends AbstractController {
    private $report_bll;
    public function handle_request() {
        $params = $this->request->get_parameters();
        $type = empty($params['type']) ? 'season' :  $params['type'];
        $end = empty($params['day']) ? 0 :  $params['day'];
        $this->report_bll = new Bll_Report_Report();
        if($end){
            $this->report_bll->set_current_month($end);
        }
        //获取权限城市+部门
        $count = $this->report_bll->check_view_auth($this->user_info['user_id']);
        if(!$count){
            $this->show_tips('ERROR_AUTH');
        }

        $data['base_data'] = $this->report_bll->get_current_dimiss($this->user_info['user_id'],$type);
        //月度时间
        $datalist = $this->report_bll->get_dimission_report($this->user_info['user_id'],$type);

        $datalist = $this->_build_data_list($datalist);
        //tp($datalist);
        if(!empty($datalist[$type])){
            $data['datalist'] = $datalist[$type];
        }else{
            $data['datalist'] = array();
        }
        $data['type'] = $type;
        $this->request->set_attribute('data', $data);
        return "Report_Dimission";
    }
    /**
     * 格式化下数据，处理key
     * @param unknown_type $datalist
     * @return multitype:
     */
    private function _build_data_list($datalist){
       $data=array();
       if(empty($datalist)){
           return $data;
       }

       foreach ($datalist as $type=>$val){
           foreach ($val as $step=> $value){
               if($type=='season'){
                   list($year,$season) = explode('-',$step);
                   $time= $year.'年'.$season.'季度';
               }else{
                   $time = date('Y年m月',strtotime($step.'01'));
               }
               $data[$type][] = array(
                       'time'=>$time,
                       'dimissRate'=>$value['dimission_rate'],
                       'total'=>100,
                       'vrate'=>$value['active_rate'],
                       'vnum'=>$value['active_all_num'],
                       'prate'=>$value['passive_rate'],
                       'pnum'=>$value['passive_all_num']
                       );
           }
       }
       $data['month'] = array_slice($data['month'], -6);
       return $data;
    }
    private function _current_quarter(){
        $month = date('n');
        $s = $month/3;
        $m = $s<1 ? 1 : ceil($s);
        $mm = ($m-1)*3+1;
        return date('Ymd',mktime(0,0,0,$mm,1,date('Y')));
    }
}